/**
 * @u kienbk1910
 * @t 4 Jun 2014
 */
package com.example.chatheadsms;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * @author kienbk1910
 *
 */
public class ChatHeadService extends Service {
	private WindowManager windowManager;
	private LayoutInflater inflater;
	private ImageView chatHead;
	TextView message ;
	View root;
 WindowManager.LayoutParams params ;
	/* (non-Javadoc)
	 * @see android.app.Service#onBind(android.content.Intent)
	 */
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	/* (non-Javadoc)
	 * @see android.app.Service#onCreate()
	 */
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
		inflater = LayoutInflater.from(this);
		root = inflater.inflate(R.layout.chathead, null);
		message=(TextView)root.findViewById(R.id.message);
		chatHead=(ImageView)root.findViewById(R.id.chatHead);
		params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
	                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
	 
	        params.gravity = Gravity.LEFT;
	        params.x=0;
	        params.y=100;
	        windowManager.addView(root, params);
	        chatHead.setOnTouchListener(new View.OnTouchListener() {
				int x,y, initx,inity;
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					x=(int)event.getRawX();
					y=(int)event.getRawX();
					switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						params.height=WindowManager.LayoutParams.MATCH_PARENT;
						params.width=WindowManager.LayoutParams.MATCH_PARENT;
						windowManager.updateViewLayout(root, params);
						  RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
						  lParams.leftMargin=0;
						  lParams.rightMargin=100;
						  v.setLayoutParams(lParams);
						message.setVisibility(View.GONE);
						initx=x-lParams.leftMargin;
						inity=y-lParams.rightMargin;
						
						   root.invalidate();
						break;
					case MotionEvent.ACTION_MOVE:
						RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
			            layoutParams.leftMargin = x - initx;
			            layoutParams.topMargin = y - inity;
			            v.setLayoutParams(layoutParams);
			            root.invalidate();
			         
						break;
					case MotionEvent.ACTION_UP:
						
						break;
					
					}
					return false;
				}
			});
		super.onCreate();
		
	}

}
